package com.sdust.user.controller;

import com.sdust.model.common.Result;
import com.sdust.common.constant.JwtClaimsConstant;
import com.sdust.common.properties.JwtProperties;
import com.sdust.model.user.dtos.UserLoginDto;
import com.sdust.model.user.entity.User;
import com.sdust.model.user.vos.UserLoginVo;
import com.sdust.common.utils.JwtUtil;
import com.sdust.user.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/service/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 测试查询用户
     * @param userName
     * @return
     */
    @GetMapping("/test")
    public Result testUser(@RequestParam String userName) {
        return Result.success(userService.testUser(userName));
    }

    /**
     * 用户登录
     * @param dto
     * @return
     */
    @PostMapping("/wxLogin")
    public Result login(@RequestBody UserLoginDto dto) {

        //微信登录
        User user = userService.wxLogin(dto);

        //为微信用户生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID,user.getId());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);

        //构建vo返回对象
        UserLoginVo userLoginVO = UserLoginVo.builder()
                .id(user.getId())
                .openid(user.getOpenid())
                .token(token)
                .avatarUrl(user.getAvatarUrl())
                .userName(user.getUserName())
                .build();

        return Result.success(userLoginVO);

    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PostMapping("/changeUserInfo")
    public Result changeUserInfo(@RequestBody User user){

        rabbitTemplate.convertAndSend("UserInfoChangeQueue", user);

        return Result.success("用户信息修改成功");
    }

    /**
     * 根据用户id查询用户信息
     * @param id
     * @return
     */
    @GetMapping("/getUserById")
    public User getUserById(@RequestParam Integer id) {
        return userService.getById(id);
    }

//    /**
//     * 测试String消息队列
//     * @return
//     */
//    @GetMapping("/testMQ")
//    public Result testMQ() {
//
//        rabbitTemplate.convertAndSend("testMq", "hello rabbitmq!");
//
//        return Result.success("消息已发送");
//    }
//
//    /**
//     * 测试String消息队列
//     * @return
//     */
//    @GetMapping("/testObjMQ")
//    public Result testObjMQ() {
//
//        User testObjMQ = User.builder().userName("testObjMQ").id(1).createTime(LocalDateTime.now()).build();
//
//        rabbitTemplate.convertAndSend("testObjMq", testObjMQ);
//
//        return Result.success("消息已发送");
//    }

}
